// i.d(t, { e: () => SettingsData })
import Data from "../object/Data"
import ObservableMap, { createMap } from "../observer/observable.map"
import localStorage from "../utils/localStorage"

export default class SettingsData extends Data {
  properties: ObservableMap<any>
  constructor() {
    super(...arguments)
    this.name = "settings"
    this.properties = createMap()
  }
  setProperty(e: string, t: any) {
    const i = this.properties.get(e)
    if (i !== t) {
      this.properties.set(e, t)
      this.commit([e])
    }
  }
  notifyPropertyObserver(e: Function, t: string) {
    e(this.properties.get(t))
  }
  setLocalStorageProperty(e: string, t: any) {
    localStorage.setItem(e, t)
    this.setProperty(e, t)
  }
  hasProperty(e: string) {
    return this.properties.has(e)
  }
  tryGetProperty<T>(e: string, normal?: T): T {
    return this.hasProperty(e) ? this.properties.get(e) : normal
  }
  getProperty(e: string) {
    if (this.hasProperty(e)) return this.properties.get(e)
    throw new Error("SettingsData: " + e + " not found")
  }
  checkUrlParam(e: string, isval: number, i = 0) {
    const n = this.tryGetProperty(e, i)
    return "number" == typeof n ? n === isval : parseInt(n, 10) === isval
  }
  iterate(e: (key: string, val?: any) => void) {
    for (const t of this.properties.keys) e(t, this.getProperty(t))
  }
}
